package com.lvchen.ui.onefunction;

import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.lvchen.R;
import com.lvchen.common.utils.UIHelper;
import com.lvchen.common.view.GalleryView;
import com.lvchen.datamodel.OneGalleryModel;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by liusai on 2016/11/12.
 */
public class OneGalleryAdapter extends BaseAdapter {

    private DisplayMetrics displayMetrics;
    private Context context;
    private LayoutInflater mInflater;
    private List<OneGalleryModel> dlist;

    private OnFristClickListener frist;
    private OnSecondClickListener second;

    public OneGalleryAdapter(Context context, List<OneGalleryModel> dlist) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        setList(dlist);
    }

    public void setList(List<OneGalleryModel> dlist){
        if(dlist!=null){
            this.dlist=dlist;
        }else {
            this.dlist=new ArrayList<OneGalleryModel>();
        }
    }

    public void change(List<OneGalleryModel> dlist){
        setList(dlist);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dlist.size();
    }

    @Override
    public Object getItem(int position) {
        return dlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if(view == null) {
            view = mInflater.inflate(R.layout.item_onegallery, null ,false);
            if(dlist.size()>1){
                view.setLayoutParams(new GalleryView.LayoutParams(getWidth(parent.getContext()),getHeight(parent.getContext())));
            }else {
                view.setLayoutParams(new GalleryView.LayoutParams(getWidth(parent.getContext()),getOneHeight(parent.getContext())));
            }
            viewHolder = new ViewHolder();
            viewHolder.rl_home_one = (RelativeLayout) view.findViewById(R.id.rl_home_one);
            viewHolder.img_homeone_logo = (RoundedImageView) view.findViewById(R.id.img_homeone_logo);
            viewHolder.tv_homeone_xiangmuname = (TextView) view.findViewById(R.id.tv_homeone_xiangmuname);
            viewHolder.tv_homeone_hangye = (TextView) view.findViewById(R.id.tv_homeone_hangye);
            viewHolder.tv_homeone_jieshao = (TextView) view.findViewById(R.id.tv_homeone_jieshao);
            viewHolder.tv_homeone_rongzijieduan = (TextView) view.findViewById(R.id.tv_homeone_rongzijieduan);
            viewHolder.mv_test1 = (View) view.findViewById(R.id.mv_test1);
            viewHolder.rl_home_two = (RelativeLayout) view.findViewById(R.id.rl_home_two);
            viewHolder.img_homeone_logo2 = (RoundedImageView) view.findViewById(R.id.img_homeone_logo2);
            viewHolder.tv_homeone_xiangmuname2 = (TextView) view.findViewById(R.id.tv_homeone_xiangmuname2);
            viewHolder.tv_homeone_hangye2 = (TextView) view.findViewById(R.id.tv_homeone_hangye2);
            viewHolder.tv_homeone_jieshao2 = (TextView) view.findViewById(R.id.tv_homeone_jieshao2);
            viewHolder.tv_homeone_rongzijieduan2 = (TextView) view.findViewById(R.id.tv_homeone_rongzijieduan2);


            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        try {
            if(dlist.get(position).getId2()==null){
                viewHolder.img_homeone_logo2.setVisibility(View.GONE);
                viewHolder.tv_homeone_xiangmuname2.setVisibility(View.GONE);
                viewHolder.tv_homeone_hangye2.setVisibility(View.GONE);
                viewHolder.tv_homeone_jieshao2.setVisibility(View.GONE);
                viewHolder.tv_homeone_rongzijieduan2.setVisibility(View.GONE);
                viewHolder.mv_test1.setVisibility(View.GONE);
            }else {
                viewHolder.img_homeone_logo2.setVisibility(View.VISIBLE);
                viewHolder.tv_homeone_xiangmuname2.setVisibility(View.VISIBLE);
                viewHolder.tv_homeone_hangye2.setVisibility(View.VISIBLE);
                viewHolder.tv_homeone_jieshao2.setVisibility(View.VISIBLE);
                viewHolder.tv_homeone_rongzijieduan2.setVisibility(View.VISIBLE);
                viewHolder.mv_test1.setVisibility(View.VISIBLE);
            }

            if(!TextUtils.isEmpty(dlist.get(position).getNewlogo())){
                ImageLoader.getInstance().displayImage(dlist.get(position).getNewlogo(), viewHolder.img_homeone_logo );
            }

            if(!TextUtils.isEmpty(dlist.get(position).getNewlogo2())){
                ImageLoader.getInstance().displayImage(dlist.get(position).getNewlogo2(), viewHolder.img_homeone_logo2 );
            }

            viewHolder.tv_homeone_xiangmuname.setText(dlist.get(position).getName());
            viewHolder.tv_homeone_xiangmuname2.setText(dlist.get(position).getName2());

            viewHolder.tv_homeone_hangye.setText(dlist.get(position).getIndustry_name());
            viewHolder.tv_homeone_hangye2.setText(dlist.get(position).getIndustry_name2());

            viewHolder.tv_homeone_jieshao.setText(dlist.get(position).getBrief());
            viewHolder.tv_homeone_jieshao2.setText(dlist.get(position).getBrief2());

            viewHolder.tv_homeone_rongzijieduan.setText(dlist.get(position).getPeriod_name());
            viewHolder.tv_homeone_rongzijieduan2.setText(dlist.get(position).getPeriod_name2());

            viewHolder.rl_home_one.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null!=frist){
                        frist.OnClick(v,position);
                    }
                }
            });

            viewHolder.rl_home_two.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null!=second){
                        second.OnClick(v,position);
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

        return view;
    }

    public int getWidth(Context context) {
        if(displayMetrics == null){
            displayMetrics = context.getResources().getDisplayMetrics();
        }
        return displayMetrics.widthPixels * 585 / 640;
    }

    public int getHeight(Context context) {
        if(displayMetrics == null){
            displayMetrics = context.getResources().getDisplayMetrics();
        }
        return getWidth(context) * 750 / 1200;
    }

    public int getOneHeight(Context context) {
        if(displayMetrics == null){
            displayMetrics = context.getResources().getDisplayMetrics();
        }
        return getWidth(context) * 400 / 1200;
    }

    private class ViewHolder {
        private RelativeLayout rl_home_one;
        private RoundedImageView img_homeone_logo;
        private TextView tv_homeone_xiangmuname,tv_homeone_hangye,tv_homeone_jieshao,tv_homeone_rongzijieduan;
        private View mv_test1;
        private RelativeLayout rl_home_two;
        private RoundedImageView img_homeone_logo2;
        private TextView tv_homeone_xiangmuname2,tv_homeone_hangye2,tv_homeone_jieshao2,tv_homeone_rongzijieduan2;

    }

    public static interface OnFristClickListener{
        public void OnClick(View v, int position);
    }

    public static interface OnSecondClickListener{
        public void OnClick(View v, int position);
    }

    public void setFrist(OnFristClickListener frist) {
        this.frist = frist;
    }

    public void setSecond(OnSecondClickListener second) {
        this.second = second;
    }

}
